package at.ac.lbg.media.vis.client.theme.view
{
	import flare.display.DirtySprite;
	import flare.display.TextSprite;
	import flare.util.Colors;
	import flare.util.Shapes;
	import flare.vis.Visualization;
	import flare.vis.data.DataList;
	import flare.vis.data.DataSprite;
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	
	import mx.collections.ArrayCollection;
	import mx.core.Application;

/**
 * Contains the Legend which is a Circle with all Subcategories.
 * 
 * */
	public class LegendContainer extends Sprite
	{
		
		public var WIDTH:Number = 220;
		public var HEIGHT:Number = 220;
		private var catStructure:ArrayCollection;
		
		private var mainCat:Category;
		private var wedgeDegrees:Number;
		private var wedgeRadian:Number;
		private var startRadian:Number;
		private var endRadian:Number;
		private var color1:uint;
		private var color2:uint;
		private var cat:Category;
		private var emptyColor:uint;
		private var color:uint;
		private var colorRange:ArrayCollection = new ArrayCollection();
		private var colorRange2:ArrayCollection = new ArrayCollection();
		private var label:TextSprite;
		private var vis:Visualization;
		private var legend:Sprite;
		
		public function LegendContainer(cs:ArrayCollection)
		{
			super();
			this.x = 150;
			this.y = 160;
			catStructure = cs;
			
			colorRange = Application.application.colorRange;
			colorRange2 = Application.application.colorRange2;
			vis = Application.application.tviz.visualization;
			legend = new Sprite();
            //draw(legend);
            addChild(legend);
            addEventListener(Event.RENDER, drawEvent);
		}
		
		public function redraw(event:Event):void {
			
			// make all vis nodes (artworks) dirty, the easiest way to get them rendered again
			if (Application.application.tviz.visualization.data) {
				var nodes:DataList = Application.application.tviz.visualization.data.nodes;
				if (nodes) {
					for (var r:int = 0; r < nodes.length; r++) {
						var n:DataSprite = nodes[r];
						n.dirty();
					}
				}
			}
			
			// remove legend sprite to get a clear canvas
			this.removeChildAt(0);
			legend = null;
			legend = new Sprite();
			addChild(legend);
			draw();
			DirtySprite.renderDirty();
		}
		
		public function drawEvent(event:Event):void {
			draw();
			// this eventlistener is removed after the initial screen draw.
			removeEventListener(Event.RENDER, drawEvent);
		}

		/**
		 * displays only the selected Categories from the Tree Menu
		 **/
        private function draw():void {
        	Application.application.selectedCats = getSelectedCats();
        	if (!Application.application.selectedCats) {
        		return;
        	}

			emptyColor = Colors.rgba(255, 255, 255);
			var mainCatsNum:uint;
			var diskSize:int = 15;
			var startDisk:int;
			var endDisk:int;
			var g:Graphics;
			
			mainCatsNum = Application.application.selectedCats.length;
			
			
			
			for (var k:int = 0; k < mainCatsNum; k++) {
				var count:int = mainCatsNum - 1 - k;
				mainCat = Application.application.selectedCats[k];
				
				wedgeDegrees = 360 / mainCat.children.length;
				wedgeRadian = Math.PI / 180 * wedgeDegrees;
				startRadian = 0;
				endRadian = wedgeRadian;
				color1 = colorRange.getItemAt(mainCat.index) as uint;
				color2 = colorRange2.getItemAt(mainCat.index) as uint;

				startDisk = (count) * diskSize + 3;
				endDisk = startDisk + diskSize;
				
				if (k == mainCatsNum - 1) {
					startDisk = 0;
				}
				

				for (var i:int = 0; i < mainCat.children.length; i++) {
					var catSprite:LegendCategory = new LegendCategory();
					legend.addChild(catSprite);
					catSprite.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
					var childCat:Category = mainCat.children[i] as Category;
					catSprite.title = mainCat.name + ": " + childCat.name;
					catSprite.catId = childCat.id;
					g = catSprite.graphics;
					g.clear();
					g.lineStyle(0.5, Colors.rgba(130, 130, 130), 0.2);
					//color = i % 2 == 0? color1 : color2;
					color = color1;
					cat = mainCat.children[i] as Category;
					g.beginFill(color, 0.8);
					Shapes.drawWedge(g, 0, 0, endDisk, startDisk, startRadian, endRadian);
					startRadian += wedgeRadian;
					endRadian += wedgeRadian;
				}
				
			}
        }
		public function onMouseOver(event:MouseEvent):void {
			var lg:LegendCategory = LegendCategory(event.target);
            lg.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
            lg.filters = [new GlowFilter(Colors.rgba(254, 254, 254), 1, 0, 0, 2, 1, true, true)];
			Application.application.catnames.text = lg.title;
			vis.data.visit(function(d:DataSprite):void {
				var aw:Artwork = d as Artwork;
				if (aw.categoryIds.contains(lg.catId)) {
					aw.highlightMe = lg.catId;
				}
			});
			DirtySprite.renderDirty();
		}
		
		private function onMouseOut(event:MouseEvent):void {
			var sprite:Sprite = Sprite(event.target);
			//sprite.parent.removeChild(label);
			sprite.filters = null;
			Application.application.catnames.text = "";
			vis.data.visit(function(d:DataSprite):void {
				var aw:Artwork = d as Artwork;
				aw.highlightMe = -1;
			});
			DirtySprite.renderDirty();
		}
		
		private function getSelectedCats():ArrayCollection {
			var selectedC:ArrayCollection = new ArrayCollection();
			for (var u:int = 0; u < catStructure.length; u++) {
				var catStruc:Category = catStructure[u] as Category;
				if (catStruc.isSelected) {
					catStruc.index = u;
					selectedC.addItem(catStruc);	
				}
			}
			return selectedC;
		}
	}
}